package com.itheima.dao;


import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;
import java.util.Map;

public interface OrderDao {
    @Insert("insert into t_order(id,member_id,orderDate,orderType,orderStatus,setmeal_id) " +
            " values (#{id},#{memberId},#{orderDate},#{orderType},#{orderStatus},#{setmealId})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public Integer submit(Order order);

    //检查是否进行了2预约设置

    //检查用户是否重复预约(同一个用户在同一天预约了同一个套餐)，如果是重复预约则无法完成再次预约

    //根据手机号进行判断是否已经是会员
    @Select("select * from t_member where phoneNumber = #{phoneNumber}")
    public Member getByMemberTelephone(String phoneNumber);

    // 没有预约的话进行 自动会员注册
    @Insert("insert into t_member (id,phoneNumber,sex,idCard,regTime,name) " +
            " values (null,#{phoneNumber},#{sex},#{idCard},#{regTime},#{name})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public Integer insertToMember(Member member);

    // 进行预约
    @Insert("insert into t_order(id,member_id,orderDate,orderType,orderStatus,setmeal_id,address) " +
            " values(null,#{memberId},#{orderDate},#{orderType},#{orderStatus},#{setmealId},#{address})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public Integer insertOrder(Order order);

    // 检查用户是否重复预约
    @Select("select * from t_order where member_id=#{memberId} and orderDate=#{orderDate} and setmeal_id=#{setmealId}")
    List<Order> getByMemberIdAndOrderDateAndSetmealId(@Param("memberId") Integer memberId, @Param("orderDate") Date orderDate, @Param("setmealId") Integer setmealId);


    //返回 预约信息
    @Select("select tm.name as member,ts.name as setmeal,tor.orderDate as orderDate,tor.orderType as orderType from  t_member tm,t_order tor,t_setmeal ts where tor.member_id=tm.id and tor.setmeal_id=ts.id and tor.id=#{orderId}")
    public Map findById(Integer orderId);

    @Insert("insert into t_order_address (order_id,address_id)\n" +
            "        values\n" +
            "        (#{order_id},#{address_id})")
    public void setOrderIdAddAddressId(Map<String, Integer> map);

    //每日到诊数
    @Select("select count(id) from t_order where orderDate = #{date} and orderStatus = '已到诊'")
    public Integer findVisitsCountByDate(String date);

    //每日未到诊数
    @Select("select count(id) from t_order where orderDate = #{date} and orderStatus = '未到诊'")
    public Integer findNotVisitsCountByDate(String date);
}
